package com.coderman.modelfly.utils;


import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 通用日期处理类
 * 
 * @author zhangwan
 * @email zhangwan@58.com Jun 10, 2010 5:03:18 PM
 */
public class DateBuilder {

	private static DateBuilder instance;

	public static final String YYYY_MM_DD = "yyyy-MM-dd";

	public static final String YYYY_MM_DD_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	protected static Logger logger = LoggerFactory.getLogger(DateBuilder.class);

	private DateBuilder() {
	}

	// public static DateBuilder getInstance() {
	// return new DateBuilder();
	// }
	public static DateBuilder getInstance() {
		if (instance == null) {
			instance = new DateBuilder();
		}
		return instance;
	}

	public static Date getDate(String sFormat, String date) {
		if (date == null || "".equals(date)) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(sFormat);
		try {
			return formatter.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 取得系统当前时间
	 * 
	 * @return String yyyy-mm-dd
	 */
	public static String getCurrentDate_Simple() {
		Calendar rightNow = Calendar.getInstance();
		int year = rightNow.get(Calendar.YEAR);
		int month = rightNow.get(Calendar.MONTH) + 1;
		int day = rightNow.get(Calendar.DATE);
		return convertDateToString(convertStringToDate(year + "-" + month + "-"
				+ day));
	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回时间字符串 yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurrentDate() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);

	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回时间字符串 yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurrentDate(String fm) {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat(fm);
		return format.format(date);

	}

	/**
	 * 获取现在时间
	 * 
	 * @return 返回时间字符串 yyyy-MM-dd HH:mm:ss
	 */
	public static String getDateByLongTime(long time) {
		Date date = new Date(time);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	public static String getCurrentDateOrder() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	public static String getCurrentDateTime() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		return format.format(date);

	}

	/**
	 * 取得系统当前时间前n个月的相对应的一天
	 * 
	 * @param month
	 *            int
	 * @return String yyyy-mm-dd
	 */
	public static String getMonthOfDayBeforeCurrentDate(int month) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -month);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 取得系统当前时间前n个月的相对应的一天
	 * 
	 * @param month
	 *            int
	 * @return String yyyy-mm-dd HH:mm:ss
	 */
	public static String getMonthOfDayBeforeCurrentTime(int month, String fm) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -month);
		return convertDateToString(
				convertStringToDate(
						"" + c.get(Calendar.YEAR) + "-"
								+ (c.get(Calendar.MONTH) + 1) + "-"
								+ c.get(Calendar.DATE) + " "
								+ c.get(Calendar.HOUR_OF_DAY) + ":"
								+ c.get(Calendar.MINUTE) + ":"
								+ c.get(Calendar.SECOND), fm), fm);
	}

	/**
	 * 取得系统当前时间后n个月的相对应的一天
	 * 
	 * @param month
	 *            int
	 * @return String yyyy-mm-dd
	 */
	public static String getMonthOfDayAfterCurrentDate(int month) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -month);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 月份+month month若为负则为几个月之前
	 * 
	 * @param month
	 * @return
	 */
	public static Date getMonthOfDayBeforeNow(int month) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -month);
		return c.getTime();
	}

	/**
	 * 获取本月第一天
	 * 
	 * @return
	 */
	public static String getFirstDayOfCurrentMonth() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		return convertDateToString(convertStringToDate(year + "-" + month + "-"
				+ day));
	}

	/**
	 * 获取本月最后一天
	 * 
	 * @return
	 */
	public static String getLastDayOfCurrentMonth() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		return convertDateToString(convertStringToDate(year + "-" + month + "-"
				+ day));
	}

	/**
	 * 获取某月的第一天
	 * 
	 * @return 返回时间长整型
	 */
	public static String getFirstDayOfMonthString(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		return convertDateToString(convertStringToDate(year + "-" + month + "-"
				+ day));
	}

	/**
	 * 获取某月的最后一天
	 * 
	 * @return 返回时间长整型
	 */
	public static String getLastDayOfMonthString(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DATE);
		return convertDateToString(convertStringToDate(year + "-" + month + "-"
				+ day));
	}

	/**
	 * 得到指定时间小时
	 */
	public static int getHour(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		return hour;
	}

	/**
	 * 获取指定时间天
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day = c.get(Calendar.DAY_OF_MONTH);
		return day;
	}

	/**
	 * 得到现在分钟
	 * 
	 * @return
	 */
	public static int getMinute() {
		Calendar c = Calendar.getInstance();
		int min = c.get(Calendar.MINUTE);
		return min;
	}

	/**
	 * 取得系统当前时间的前n天
	 * 
	 * @param day
	 *            int
	 * @return String yyyy-mm-dd
	 */
	public static String getDayBeforeCurrentDate(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -day);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	public static String getDayBeforeCurrentDate(int day, String fm) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -day);
		return convertDateToString(
				convertStringToDate(
						"" + c.get(Calendar.YEAR) + "-"
								+ (c.get(Calendar.MONTH) + 1) + "-"
								+ c.get(Calendar.DATE) + " "
								+ c.get(Calendar.HOUR_OF_DAY) + ":"
								+ c.get(Calendar.MINUTE) + ":"
								+ c.get(Calendar.SECOND), fm), fm);
	}

	/**
	 * 取得系统当前时间后n天
	 * 
	 * @param day
	 *            int
	 * @return String yyyy-mm-dd
	 */
	public static String getDayAfterCurrentDate(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, day);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 取得系统当前时间后n天
	 * 
	 * @param date
	 * @param day
	 * @return String yyyy-mm-dd
	 */
	public static Date getDayAfterCurrentDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, day);
		return c.getTime();
	}

	/**
	 * 取得系统当前时间后n天
	 * 
	 * @param day
	 * @return String yyyymmdd
	 */
	public static String getDayAfterCurrentDateInt(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, day);
		String month = (c.get(Calendar.MONTH) + 1) + "";
		String date = c.get(Calendar.DATE) + "";
		if ((c.get(Calendar.MONTH) + 1) < 10) {
			month = "0" + month;
		}
		if (c.get(Calendar.DATE) < 10) {
			date = "0" + date;
		}
		return c.get(Calendar.YEAR) + month + date;
	}

	/**
	 * 取得系统当前时间后n天
	 * 
	 * @param day
	 * @return String mm.dd
	 */
	public static String getDayAfterCurrentDateNew(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, day);
		return (c.get(Calendar.MONTH) + 1) + "." + c.get(Calendar.DATE);
	}

	/**
	 * 
	 * @Description: 给日期加月份
	 * @param @param date
	 * @param @param addNum
	 * @param @return
	 * @return Date
	 * @throws
	 */
	public static Date getDateAddMonth(Date date, int addNum) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, addNum);
		return calendar.getTime();
	}

	/**
	 * 取得系统当前时间后n天
	 * 
	 * @param day
	 *            int
	 * @return String yyyy-mm-dd hh:MM:ss
	 */
	public static Date getDayAfterCurrentTime(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, day);
		return convertStringToDate("" + c.get(Calendar.YEAR) + "-"
				+ (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DATE)
				+ " " + Calendar.HOUR + ":" + Calendar.MINUTE + ":"
				+ Calendar.SECOND);
	}

	public static Date getDayAfterCurrentDateByDate(int day) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, day);
		return convertStringToDate("" + c.get(Calendar.YEAR) + "-"
				+ (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DATE));
	}

	public static Date getDateTimeAfterCurrentDate(int day)
			throws ParseException {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
		String newDate = getDayAfterCurrentDate(day);
		String time = format.format(date);
		SimpleDateFormat formatTime = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return formatTime.parse(newDate + " " + time);
	}

	/**
	 * 获取日期的星期
	 * 
	 * @param date
	 *            日期
	 * @return 星期数字
	 */
	public static int getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(Calendar.DAY_OF_WEEK) - 1;
		return week;

	}

	public static String getWeekString(int week) {
		String strWeek = "";
		switch (week) {
		case 0:
			strWeek = "星期日";
			break;
		case 1:
			strWeek = "星期一";
			break;
		case 2:
			strWeek = "星期二";
			break;
		case 3:
			strWeek = "星期三";
			break;
		case 4:
			strWeek = "星期四";
			break;
		case 5:
			strWeek = "星期五";
			break;
		case 6:
			strWeek = "星期六";
			break;
		case 7:
			strWeek = "星期日";
			break;
		default:
			strWeek = "";
		}
		return strWeek;
	}

	// type=0,星期，type=1,周
	public static String getWeekString(int week, int type) {
		if (type == 0) {
			return getWeekString(week);
		}
		String strWeek = "";
		switch (week) {
		case 0:
			strWeek = "周日";
			break;
		case 1:
			strWeek = "周一";
			break;
		case 2:
			strWeek = "周二";
			break;
		case 3:
			strWeek = "周三";
			break;
		case 4:
			strWeek = "周四";
			break;
		case 5:
			strWeek = "周五";
			break;
		case 6:
			strWeek = "周六";
			break;
		case 7:
			strWeek = "周日";
			break;
		default:
			strWeek = "";
		}
		return strWeek;
	}

	/**
	 * 将一个日期字符串转化成日期
	 * 
	 * @param strDate
	 *            String
	 * @return Date yyyy-mm-dd
	 */
	public static Date convertStringToDate(String strDate) {
		Date date = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			date = df.parse(strDate);

		} catch (Exception e) {

			logger.error("日期转换失败:" + e.getMessage());
		}
		return date;
	}

	public static Date convertStringToDate(String strDate, String fm) {
		Date date = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat(fm);
			date = df.parse(strDate);

		} catch (Exception e) {
			logger.error("日期转换失败:" + e.getMessage(), e);
		}
		return date;
	}

	public static Date convertStringToDate2(String strDate) {
		Date date = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S");
			date = df.parse(strDate);

		} catch (Exception e) {
			logger.error("日期转换失败:" + e.getMessage(), e);
		}
		return date;
	}

	public static Date convertStringToDate3(String strDate) {
		Date date = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = df.parse(strDate);

		} catch (Exception e) {
			logger.error("日期转换失败:" + e.getMessage(), e);
		}
		return date;
	}

	public static Date convertStringToDate4(String strDate) {
		Date date = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = df.parse(strDate);

		} catch (Exception e) {
			// logger.error("日期转换失败:" + e.getMessage(), e);
		}
		return date;
	}

	/**
	 * 输入两个字符串型的日期，比较两者的大小
	 * 
	 * @param fromDate
	 *            String
	 * @param toDate
	 *            String
	 * @return boolean before为true
	 */
	public static boolean compareTwoDateBigOrSmall(String fromDate,
			String toDate) {
		Date dateFrom = convertStringToDate(fromDate);
		Date dateTo = convertStringToDate(toDate);
		if (dateFrom.before(dateTo)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 输入两个字符串型的日期，比较两者的大小
	 * 
	 * @param Date1
	 *            String
	 * @param Date2
	 *            String
	 * @return -1 1<2 0 1=2 1 1>2 else -2
	 */
	public static int compareDate(String Date1, String Date2) {
		Date date1 = new Date();
		Date date2 = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date1 = df.parse(Date1);
			date2 = df.parse(Date2);
			if (date1.before(date2)) {
				return -1;
			} else if (date1.equals(date2)) {
				return 0;
			} else if (date1.after(date2)) {
				return 1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return -2;
	}

	/**
	 * 将一个日期字符串转化成Calendar
	 * 
	 * @param strDate
	 *            String
	 * @return Calendar
	 */
	public static Calendar convertDateStringToCalendar(String strDate) {
		Date date = convertStringToDate(strDate);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

	public static Calendar convertDateStringToCalendar(String strDate, String fm) {
		Date date = convertStringToDate(strDate, fm);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

	/**
	 * 将一个日期转化成Calendar
	 * 
	 * @param date
	 *            Date
	 * @return Calendar
	 */
	public static Calendar convertDateToCalendar(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

	/**
	 * 取得某个特定时间前n个月相对应的一天
	 * 
	 * @param strDate
	 *            String
	 * @param month
	 *            int
	 * @return yyyy-mm-dd
	 */
	public static String getDayOfMonthBeforeSpecificDate(String strDate,
			int month) {
		Calendar c = convertDateStringToCalendar(strDate);
		c.add(Calendar.MONTH, -month);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 取得某个特定时间前N小时的时间
	 * 
	 * @param strDate
	 *            String
	 * @param hour
	 *            int
	 * @return yyyy-mm-dd HH:mm:ss
	 */
	public static String getDayOfHourBefore(String strDate, int hour) {

		Date date = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = df.parse(strDate);
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.add(Calendar.HOUR, -hour);
			strDate = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
					+ "-" + c.get(Calendar.DATE) + " "
					+ c.get(Calendar.HOUR_OF_DAY) + ":"
					+ c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
		} catch (Exception e) {
			logger.error("日期转换失败:" + e.getMessage(), e);
		}
		return strDate;
	}

	/**
	 * 取得某个特定时间后n个月相对应的一天
	 * 
	 * @param strDate
	 *            String
	 * @param month
	 *            int
	 * @return yyyy-mm-dd
	 */
	public static String getDayOfMonthAfterSpecificDate(String strDate,
			int month) {
		Calendar c = convertDateStringToCalendar(strDate);
		c.add(Calendar.MONTH, month);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 取得某个特定时间前n天,格式为yyyy-mm-dd
	 * 
	 * @param strDate
	 *            String
	 * @param day
	 *            int
	 * @return yyyy-mm-dd
	 */
	public static String getDayBeforeSpecificDate(String strDate, int day) {
		Calendar c = convertDateStringToCalendar(strDate);
		c.add(Calendar.DAY_OF_MONTH, -day);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 取得某个时间后n天,格式为yyyy-mm-dd
	 * 
	 * @param strDate
	 *
	 * @param day
	 *            int
	 * @return yyyy-mm-dd
	 */
	public static String getDayAfterSpecificDate(String strDate, int day) {
		Calendar c = convertDateStringToCalendar(strDate);
		c.add(Calendar.DAY_OF_MONTH, day);
		return convertDateToString(convertStringToDate(""
				+ c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1)
				+ "-" + c.get(Calendar.DATE)));
	}

	/**
	 * 判断系统当前时间是否为闰年
	 * 
	 * @return
	 */
	public boolean isLeapYear() {
		Calendar rightNow = Calendar.getInstance();
		int year = rightNow.get(Calendar.YEAR);
		if (0 == year % 4 && (year % 100 != 0 || year % 400 == 0)) {
			return true;
		} else {
			return false;
		}

	}

	/**
	 * 将一个字符串转化为标准日期
	 *
	 * @param str
	 * @return
	 */
	public Date convertStringToStandardDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		Date dateTime = null;
		if (!"".equals(str)) {
			try {
				date = sdf.parse(str);
				dateTime = new Date(date.getTime());
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
		return dateTime;
	}

	/**
	 * 将日期转化为字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String convertDateToString(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

	public static String convertDateToString(Date date, String fm) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(fm);
		return dateFormat.format(date);
	}

	/**
	 * 将日期转化为字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String convertDateToString_str(Date date) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		return dateFormat.format(date);
	}

	/**
	 * 将日期转换为字符串
	 * 
	 * @param date
	 *            日期
	 * @param pattern
	 *            格式字符串
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		if (date == null) {
			return "";
		}
		if (pattern == null || "".equals(pattern)) {
			pattern = "yyyy-MM-dd";
		}
		SimpleDateFormat dateFormat = null;
		try {
			dateFormat = new SimpleDateFormat(pattern);
			return dateFormat.format(date);
		} catch (Exception e) {
			dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			return dateFormat.format(date);
		}

	}

	/**
	 * 获取指定时间月份
	 */
	public static int getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH) + 1;
		return month;
	}

	/**
	 * 获取指定时间年份
	 */
	public static int getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 取得上一个小时
	 */
	public static String getPreHour(String format, int step) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) - step);
		return sdf.format(cal.getTime());
	}

	/**
	 * 
	 * 取得某日期的上一个月时间
	 */
	public static String getPrevMonthOfDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(convertStringToDate(date));
		cal.add(Calendar.MONTH, -1); // 前个月
		return sdf.format(cal.getTime());
	}

	/**
	 * 
	 * 取得某日期的下一个月时间
	 */
	public static String getNextMonthOfDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(convertStringToDate(date));
		cal.add(Calendar.MONTH, 1); // 后个月
		return sdf.format(cal.getTime());
	}

	public static Hashtable<Integer, Integer> getWeekNumberHT(long beginDate,
			long endDate) {
		Hashtable<Integer, Integer> weeks = new Hashtable<Integer, Integer>();
		for (int i = 1; i <= 7; i++) {
			weeks.put(i, 0);
		}
		long q = beginDate;
		for (q = beginDate; q <= endDate; q = q
				+ new Long(24 * 3600 * 1000).longValue()) {
			Calendar c = Calendar.getInstance();
			Date date = new Date(q);
			c.setTime(date);
			int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
			if (dayofweek == 0) {
				dayofweek = 7;
			}
			if (weeks.containsKey(dayofweek)) {
				weeks.put(dayofweek, weeks.get(dayofweek) + 1);
			} else {
				weeks.put(dayofweek, 1);
			}
		}
		return weeks;
	}

	/**
	 * 上一星期
	 * 
	 * @param date
	 * @return
	 */
	public static String getPrevWeekOfDate(String date) {
		return getDayBeforeSpecificDate(date, 7);
	}

	/**
	 * 获取某年某月的最后一天
	 * 
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @return 最后一天
	 */
	public static int getLastDayOfMonth(int year, int month) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			return 30;
		}
		if (month == 2) {
			if (isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		}
		return 0;
	}

	/**
	 * 是否闰年
	 * 
	 * @param year
	 *            年
	 * @return
	 */
	public static boolean isLeapYear(int year) {
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}

	/**
	 * 返回某段时间内 每一天
	 * 
	 * @param beginDateStr
	 * @param endDateStr
	 * @return
	 */
	public static List<String> getDaysByFromTo(String beginDateStr,
			String endDateStr) {
		Date beiginDate = convertStringToDate(beginDateStr);
		endDateStr = getDayAfterSpecificDate(endDateStr, 1);
		Date endDate = convertStringToDate(endDateStr);
		List<String> dateList = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date currentDate = new Date();
		if (currentDate.before(beiginDate)) {
			currentDate = beiginDate;
		}
		String currentDateStr = sdf.format(currentDate);
		while (currentDate.before(endDate)) {
			dateList.add(currentDateStr);
			currentDateStr = getDayAfterSpecificDate(currentDateStr, 1);
			currentDate = convertStringToDate(currentDateStr);
		}
		return dateList;
	}

	public static boolean isSameDay(Date day1, Date day2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String ds1 = sdf.format(day1);
		String ds2 = sdf.format(day2);
		if (ds1.equals(ds2)) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean compareDay(Date day1, Date day2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String ds1 = sdf.format(day1);
		String ds2 = sdf.format(day2);

		if (convertStringToDate(ds1).before(convertStringToDate(ds2))) {
			return true;
		} else {
			return false;
		}
	}

	public static int getAge(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
					"The birthDay is before Now.It’s unbelievable!");
		}

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH) + 1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {

				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				age--;
			}
		}

		return age;
	}

	public static int getAgeFromYear(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();

		if (cal.before(birthDay)) {
			throw new IllegalArgumentException(
					"The birthDay is before Now.It’s unbelievable!");
		}

		int yearNow = cal.get(Calendar.YEAR);

		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);

		int age = yearNow - yearBirth;

		return age;
	}

	/**
	 * 判断是否是工作日 目前只有周六周日算不算工作日
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static boolean isWorkingDay(String dateStr) {

		boolean isWorkingDay = false;

		if (null == dateStr || "" == dateStr) {
			return isWorkingDay;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			Date date = sdf.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			isWorkingDay = isWorkingDay(calendar);
		} catch (ParseException e) {
			logger.error("日期转换失败:" + e.getMessage(), e);
		}

		return isWorkingDay;
	}

	/**
	 * 判断是否是工作日 目前只有周六周日算不算工作日
	 * 
	 * @param Calendar
	 *            calendar
	 * @return
	 */
	public static boolean isWorkingDay(Calendar calendar) {
		boolean isWorkingDay = false;

		if (null == calendar) {
			return isWorkingDay;
		}

		if (calendar.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SATURDAY
				&& calendar.get(GregorianCalendar.DAY_OF_WEEK) != GregorianCalendar.SUNDAY) {
			isWorkingDay = true;
		}
		return isWorkingDay;
	}

	/**
	 * 校验时间字符串是否符合yyyy-MM-dd HH:mm:ss格式
	 * 
	 * @param dateStr
	 * @return
	 */
	private static  Pattern p = Pattern
			.compile("^((((21|20|19)[0-3][0-9]-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|(20[0-3][0-9]-(0[2469]|11)-(0[1-9]|[12][0-9]|30))) (20|21|22|23|[0-1][0-9]):[0-5][0-9]:[0-5][0-9])$");
	public static boolean checkTime(String dateStr) {

		if (!(null == dateStr) && !"".equals(dateStr)) {
			dateStr = dateStr.trim();
		}
		Matcher match = p.matcher(dateStr);
		return match.find();
	}

	public static Map<String, String> weekdaymap = new HashMap<String, String>();
	static {
		weekdaymap.put("1", "周日");
		weekdaymap.put("2", "周一");
		weekdaymap.put("3", "周二");
		weekdaymap.put("4", "周三");
		weekdaymap.put("5", "周四");
		weekdaymap.put("6", "周五");
		weekdaymap.put("7", "周六");
	}

	public static String formatDateWithWeekday(Date d) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ("
				+ weekdaymap.get(cale.get(Calendar.DAY_OF_WEEK) + "")
				+ ") HH:mm");
		return sdf.format(d);
	}

	public static String formatDateWithWeekdayNoHour(Date d) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ("
				+ weekdaymap.get(cale.get(Calendar.DAY_OF_WEEK) + "") + ")");
		return sdf.format(d);
	}

	/**
	 * @author 宣浩
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Integer getMonthDif(Date date1, Date date2) {
		//DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();

		c.setTime(date1);

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		c.setTime(date2);
		int year1 = c.get(Calendar.YEAR);
		int month1 = c.get(Calendar.MONTH);
		int day1 = c.get(Calendar.DAY_OF_MONTH);
		int result;
		if (year == year1) {

			result = month1 - month;// 两个日期相差几个月，即月份差
		} else {
			result = 12 * (year1 - year) + month1 - month;// 两个日期相差几个月，即月份差
		}
		if(day1-day<0){
			result = result-1;
		}
		return result;
	}

	public static String getDateFromStr(String serviceTime) {

		if (StringUtils.isEmpty(serviceTime)) {
			return null;
		}

		Date date = null;
		SimpleDateFormat sdf = null;
		try {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = sdf.parse(serviceTime);
		} catch (ParseException e) {
			try {
				sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				date = sdf.parse(serviceTime);
			} catch (ParseException e1) {
				try {
					sdf = new SimpleDateFormat("yyyy-MM-dd HH");
					date = sdf.parse(serviceTime);
				} catch (ParseException e2) {
					try {
						sdf = new SimpleDateFormat("yyyy-MM-dd");
						date = sdf.parse(serviceTime);
					} catch (ParseException e3) {
						return null;
					}
				}
			}
		}

		return DateBuilder.convertDateToString(date, "yyyy-MM-dd HH:mm:ss");
	}

	// 根据日期获取星期
	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekDays[w];
	}

	/**
	 * 
	 * @Description:获取日期月份开始
	 * @author lizhenxing
	 * @param date
	 * @return
	 * @return Date
	 * @date 2016年5月30日 下午5:58:38
	 */
	public static Date getMonthStartDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	/**
	 * 
	 * @Description:获取日期月份结束
	 * @author lizhenxing
	 * @param date
	 * @return
	 * @return Date
	 * @date 2016年5月30日 下午5:58:38
	 */
	public static Date getMonthEndDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		return c.getTime();
	}

	/**
	 * 
	 * @Description:加年份
	 * @author lizhenxing
	 * @param date
	 * @param addNum
	 * @return
	 * @return Date
	 * @date 2016年5月30日 下午6:03:04
	 */
	public static Date getYearAdd(Date date, int addNum) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, addNum);
		return calendar.getTime();
	}

	/**
	 * 
	 * @Description:加天
	 * @author lizhenxing
	 * @param date
	 * @param addNum
	 * @return
	 * @return Date
	 * @date 2016年5月30日 下午6:03:04
	 */
	public static Date getDayAdd(Date date, int addNum) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, addNum);
		return calendar.getTime();
	}
	public static void main(String[] args) throws ParseException {
		System.out.println(getMonthDif(new SimpleDateFormat("yyyy-MM-dd").parse("2016-06-24"),new Date()));
	}
}
